﻿    local addon = CreateFrame("Frame", nil, UIParent)
  
-- XXXXXXXX SETTINGS START XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  -- Getting the player class
  local localizedplayerclass , playerclass = UnitClass("player")

  -- for druid shapeshifting, we need gloabals. Haven't found a convenient way around that yet.
  
  shapeold = 0
  shapenew = 0
  
  --size the ORB
  local orbsize = 130

  --POSITION THE HEALTH ORB
  local healthorbposanchor = "BOTTOM"
  local healthorbposx = -274
  local healthorbposy = -8
  
  --POSITION THE MANA ORB
  local manaorbposanchor = "BOTTOM"
  local manaorbposx = 274
  local manaorbposy = -8
  

-- XXXXXXXX SETTINGS END XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
 
  addon:RegisterEvent("PLAYER_LOGIN")
  
  addon:SetScript("OnEvent", function ()
    if(event=="PLAYER_LOGIN") then
      addon:create_d3orbs()
    end 
  end)

  function addon:create_d3orbs()
  
    --LIFE ORB
    local f = CreateFrame("Frame","D3Orb2HealthFrame",UIParent)
    f:SetFrameStrata("BACKGROUND")
    f:SetFrameLevel(1)
    f:SetWidth(orbsize)
    f:SetHeight(orbsize)
    f:SetPoint(healthorbposanchor,healthorbposx,healthorbposy)
    f:Show()
    
    local t1 = f:CreateTexture("D3Orb2HealthBack", "BACKGROUND")
    t1:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_back.tga")
    t1:SetAllPoints(f)
    
    local t2 = f:CreateTexture("D3Orb2HealthFilling","ARTWORK")
    t2:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_life.tga")
    t2:SetPoint("BOTTOMLEFT",0,0)
    t2:SetWidth(orbsize)
    t2:SetHeight(orbsize)
    t2:SetVertexColor(0.2,0.2,0.2)


        
    local pm1 = CreateFrame("PlayerModel", "D3Orb2HealthFog1",f)
    pm1:SetFrameStrata("BACKGROUND")
    pm1:SetFrameLevel(2)
    pm1:SetWidth(orbsize)
    pm1:SetHeight(orbsize)
    pm1:SetPoint("TOPRIGHT",-5,-10)
    pm1:SetPoint("BOTTOMLEFT",10,5)
    pm1:SetModel("SPELLS\\RedRadiationFog.m2")
    pm1:SetPosition(-5, 0.5, 0.5) 

    pm1:SetScript("OnShow",function() 
      pm1:SetModel("SPELLS\\RedRadiationFog.m2")
      pm1:SetPosition(-5, 0.5, 0.5) 
    end)

    --[[
    local pm2 = CreateFrame("PlayerModel", "D3Orb2HealthFog2",f)
    pm2:SetFrameStrata("BACKGROUND")
    pm2:SetFrameLevel(2)
    pm2:SetPoint("BOTTOMLEFT",5,5)
    pm2:SetPoint("TOPRIGHT",-5,-5)
    pm2:SetModel("SPELLS\\RedRadiationFog.m2")
    pm2:SetPosition(0, 0, 1.1) 

    pm2:SetScript("OnShow",function() 
      pm2:SetModel("SPELLS\\RedRadiationFog.m2")
      pm2:SetPosition(0, 0, 1.1) 
    end)    
    
    local pm3 = CreateFrame("PlayerModel", "D3Orb2HealthFog3",f)
    pm3:SetFrameStrata("BACKGROUND")
    pm3:SetFrameLevel(2)
    pm3:SetPoint("BOTTOMLEFT",5,5)
    pm3:SetPoint("TOPRIGHT",-5,-5)
    pm3:SetModel("SPELLS\\RedRadiationFog.m2")
    pm3:SetPosition(0, 0, 1.1) 

    pm3:SetScript("OnShow",function() 
      pm3:SetModel("SPELLS\\RedRadiationFog.m2")
      pm3:SetPosition(0, 0, 1.1) 
    end)  
    ]]--

    
    local f3 = CreateFrame("Frame","D3Orb2HealthFrameHelper",UIParent)
    f3:SetFrameStrata("BACKGROUND")
    f3:SetFrameLevel(3)
    f3:SetAllPoints(f) 
    f3:Show()

    local t3 = f3:CreateTexture("D3Orb2HealthGloss", "OVERLAY")
    t3:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_gloss.tga")
    t3:SetAllPoints(f3)    
    
    --MANA ORB
    local f2 = CreateFrame("Frame","D3Orb2ManaFrame", UIParent)
    f2:SetFrameStrata("BACKGROUND")
    f2:SetFrameLevel(1)
    f2:SetWidth(orbsize)
    f2:SetHeight(orbsize)
    f2:SetPoint(manaorbposanchor,manaorbposx,manaorbposy)
    f2:Show()
    
    local t4 = f2:CreateTexture("D3Orb2ManaBack", "BACKGROUND")
    t4:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_back.tga")
    t4:SetAllPoints(f2)

    local t5 = f2:CreateTexture("D3Orb2ManaFilling","ARTWORK")
    if (playerclass == "ROGUE") then
      t5:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_energy.tga")
    elseif (playerclass == "WARRIOR") then
      t5:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_life.tga")
    else
      t5:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_mana.tga")
    end
    t5:SetPoint("BOTTOMLEFT",0,0)
    t5:SetWidth(orbsize)
    t5:SetHeight(orbsize) 
    t5:SetVertexColor(0.2,0.2,0.2)
    
    
    local pm4 = CreateFrame("PlayerModel", "D3Orb2ManaFog1",f2)
    pm4:SetFrameStrata("BACKGROUND")
    pm4:SetFrameLevel(2)
    pm4:SetPoint("BOTTOMLEFT",5,5)
    pm4:SetPoint("TOPRIGHT",-10,-10)
    if (playerclass == "ROGUE") then
      pm4:SetModel("SPELLS\\OrangeRadiationFog.m2")
    elseif (playerclass == "WARRIOR") then
      pm4:SetModel("SPELLS\\RedRadiationFog.m2")
    else
      pm4:SetModel("SPELLS\\BlueRadiationFog.m2")
    end
    --pm4:SetModel("SPELLS\\BlueRadiationFog.m2")
    pm4:SetPosition(-5, 0, 0.5) 

    pm4:SetScript("OnShow",function() 
      if (playerclass == "ROGUE") then
        pm4:SetModel("SPELLS\\OrangeRadiationFog.m2")
      elseif (playerclass == "WARRIOR") then
        pm4:SetModel("SPELLS\\RedRadiationFog.m2")
      else
        pm4:SetModel("SPELLS\\BlueRadiationFog.m2")
      end
      if (playerclass == "DRUID") then
	  shapenew = GetShapeshiftForm(True);
	  if shapenew == 1 then
	    t5:SetTexture("Interface\\AddOns\\Diablo3Orbs\\orb_life.tga")
	    pm4:SetModel("SPELLS\\RedRadiationFog.m2")
	  elseif (shapenew == 3) or ((shapenew == 2) and (GetNumShapeshiftForms() == 2)) then
	    t5:SetTexture("Interface\\AddOns\\Diablo3Orbs\\orb_energy.tga")
	    pm4:SetModel("SPELLS\\OrangeRadiationFog.m2")
	  else
	    t5:SetTexture("Interface\\AddOns\\Diablo3Orbs\\orb_mana.tga")
	    pm4:SetModel("SPELLS\\BlueRadiationFog.m2")
	  end
	end
      pm4:SetPosition(-5, 0, 0.5) 
    end)

    --[[
    local pm5 = CreateFrame("PlayerModel", "D3Orb2ManaFog2",f2)
    pm5:SetFrameStrata("BACKGROUND")
    pm5::SetFrameLevel(2)
    pm5:SetPoint("BOTTOMLEFT",5,5)
    pm5:SetPoint("TOPRIGHT",-5,-5)
    pm5:SetModel("SPELLS\\BlueRadiationFog.m2")
    pm5:SetPosition(0, 0, 1.1) 

    pm5:SetScript("OnShow",function() 
      pm5:SetModel("SPELLS\\BlueRadiationFog.m2")
      pm5:SetPosition(0, 0, 1.1) 
    end)    
    
    local pm6 = CreateFrame("PlayerModel", "D3Orb2ManaFog3",f2)
    pm6:SetFrameStrata("BACKGROUND")
    pm6:SetFrameLevel(2)
    pm6:SetPoint("BOTTOMLEFT",5,5)
    pm6:SetPoint("TOPRIGHT",-5,-5)
    pm6:SetModel("SPELLS\\BlueRadiationFog.m2")
    pm6:SetPosition(0, 0, 1.1) 

    pm6:SetScript("OnShow",function() 
      pm6:SetModel("SPELLS\\BlueRadiationFog.m2")
      pm6:SetPosition(0, 0, 1.1)
    end)
    ]]--    
        
    local f4 = CreateFrame("Frame","D3Orb2ManaFrameHelper",UIParent)
    f4:SetFrameStrata("BACKGROUND")
    f4:SetFrameLevel(3)
    f4:SetAllPoints(f2) 
    f4:Show()
    
    local t6 = f4:CreateTexture("D3Orb2ManaGloss", "OVERLAY")
    t6:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_gloss.tga")
    t6:SetAllPoints(f4)
    
    
    -- XXXXXXXXXXX HP and MP STUFF XXXXXXXXXXXXXXXXXXXXXXx

    f:SetScript("OnEvent", function(frame, event, unit)
      if event == "UNIT_HEALTH" and unit == "player" then
        	t2:SetHeight((UnitHealth("player") / UnitHealthMax("player")) * orbsize)
        t2:SetTexCoord(0,1,  math.abs(UnitHealth("player") / UnitHealthMax("player") - 1),1)
        diabloOrbHealthText:SetText(floor(((UnitHealth("player") / UnitHealthMax("player"))*100)+.5).."%")
        diabloOrbHealthText2:SetText(UnitHealth("player").."/"..UnitHealthMax("player"))
        pm1:SetAlpha(UnitHealth("player") / UnitHealthMax("player"))
      elseif event == "PLAYER_ENTERING_WORLD" then
        t2:SetHeight((UnitHealth("player") / UnitHealthMax("player")) * orbsize)
        t2:SetTexCoord(0,1,  math.abs(UnitHealth("player") / UnitHealthMax("player") - 1),1)      
        diabloOrbHealthText:SetText(floor(((UnitHealth("player") / UnitHealthMax("player"))*100)+.5).."%")
        diabloOrbHealthText2:SetText(UnitHealth("player").."/"..UnitHealthMax("player"))
        pm1:SetAlpha(UnitHealth("player") / UnitHealthMax("player"))
        pm1:SetModel("SPELLS\\RedRadiationFog.m2")
        --pm1:SetPosition(0, 0, 1) 
      elseif event == "PLAYER_LOGIN" then
        t2:SetHeight((UnitHealth("player") / UnitHealthMax("player")) * orbsize)
        t2:SetTexCoord(0,1,  math.abs(UnitHealth("player") / UnitHealthMax("player") - 1),1)      
        diabloOrbHealthText:SetText(floor(((UnitHealth("player") / UnitHealthMax("player"))*100)+5).."%")
        diabloOrbHealthText2:SetText(UnitHealth("player").."/"..UnitHealthMax("player"))
        pm1:SetAlpha(UnitHealth("player") / UnitHealthMax("player"))
      end    
    end)
    f:RegisterEvent("UNIT_HEALTH")
    f:RegisterEvent("PLAYER_ENTERING_WORLD")
    f:RegisterEvent("PLAYER_LOGIN")
    
    
    f2:SetScript("OnEvent", function(frame, event, unit)
      shapenew = GetShapeshiftForm(True);
      	if (playerclass == "DRUID") and (shapeold ~= shapenew) then
	  if shapenew == 1 then
	    t5:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_life.tga")
	    pm4:SetModel("SPELLS\\RedRadiationFog.m2")
	  elseif (shapenew == 3) or ((shapenew == 2) and (GetNumShapeshiftForms() == 2)) then
	    t5:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_energy.tga")
	    pm4:SetModel("SPELLS\\OrangeRadiationFog.m2")
	  else
	    t5:SetTexture("Interface\\Addons\\Diablo3dynOrbs\\orb_mana.tga")
	    pm4:SetModel("SPELLS\\BlueRadiationFog.m2")
	  end
	end
      if (event == "UNIT_MANA" or event == "UNIT_RAGE" or event == "UNIT_ENERGY" or event == "UNIT_RUNIC_POWER") and unit == "player" then
        t5:SetHeight((UnitMana("player") / UnitManaMax("player")) * orbsize)
        t5:SetTexCoord(0,1,  math.abs(UnitMana("player") / UnitManaMax("player") - 1),1)
        diabloOrbManaText:SetText(floor((UnitMana("player") / UnitManaMax("player"))*100).."%")
        diabloOrbManaText2:SetText(UnitMana("player").."/"..UnitManaMax("player"))
        pm4:SetAlpha(UnitMana("player") / UnitManaMax("player"))
	      elseif event == "PLAYER_ENTERING_WORLD" then
        t5:SetHeight((UnitMana("player") / UnitManaMax("player")) * orbsize)
        t5:SetTexCoord(0,1,  math.abs(UnitMana("player") / UnitManaMax("player") - 1),1)    
        diabloOrbManaText:SetText(floor((UnitMana("player") / UnitManaMax("player"))*100).."%")
        diabloOrbManaText2:SetText(UnitMana("player").."/"..UnitManaMax("player"))
        pm4:SetAlpha(UnitMana("player") / UnitManaMax("player"))
	if (playerclass == "ROGUE") then
          pm4:SetModel("SPELLS\\OrangeRadiationFog.m2")
        elseif (playerclass == "WARRIOR") then
          pm4:SetModel("SPELLS\\RedRadiationFog.m2")
        elseif	(playerclass == "DRUID") then
	  t5:SetTexture("Interface\\AddOns\\Diablo3Orbs\\orb_mana.tga")
	  pm4:SetModel("SPELLS\\BlueRadiationFog.m2")
	  if shapenew == 1 then
	    t5:SetTexture("Interface\\AddOns\\Diablo3Orbs\\orb_life.tga")
	    pm4:SetModel("SPELLS\\RedRadiationFog.m2")
	  elseif (shapenew == 3) or ((shapenew == 2) and (GetNumShapeshiftForms() == 2)) then
	    t5:SetTexture("Interface\\AddOns\\Diablo3Orbs\\orb_energy.tga")
	    pm4:SetModel("SPELLS\\OrangeRadiationFog.m2")
	  end	 
	else
          pm4:SetModel("SPELLS\\BlueRadiationFog.m2")
	end
        --pm4:SetModel("SPELLS\\BlueRadiationFog.m2")
        --pm4:SetPosition(0, 1, 1.1) 
      elseif event == "PLAYER_LOGIN" then
        t5:SetHeight((UnitMana("player") / UnitManaMax("player")) * orbsize)
        t5:SetTexCoord(0,1,  math.abs(UnitMana("player") / UnitManaMax("player") - 1),1)    
        diabloOrbManaText:SetText(floor((UnitMana("player") / UnitManaMax("player"))*100).."%")
        diabloOrbManaText2:SetText(UnitMana("player").."/"..UnitManaMax("player"))
        pm4:SetAlpha(UnitMana("player") / UnitManaMax("player"))
      end  
      shapeold = shapenew
    end)
    f2:RegisterEvent("UNIT_MANA")
    f2:RegisterEvent("UNIT_RAGE")
    f2:RegisterEvent("UNIT_ENERGY")
    f2:RegisterEvent("UNIT_RUNIC_POWER")
    f2:RegisterEvent("PLAYER_ENTERING_WORLD")
    f2:RegisterEvent("PLAYER_LOGIN")
    f2:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
    
    -- XXXXXXXXXXXXX TEXT STUFF XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      
    diabloOrbHealthText = diabloOrbHealthText or f:CreateFontString("diabloOrbHealthText", "OVERLAY")
    diabloOrbHealthText:SetPoint("CENTER",0,10)
    diabloOrbHealthText:SetFont(NAMEPLATE_FONT, 16, "THINOUTLINE")
    diabloOrbHealthText:SetTextColor(1, 1, 1)
    diabloOrbHealthText:SetText("100%")
    diabloOrbHealthText:SetAlpha(1)
    --diabloOrbHealthText:Hide()
    diabloOrbHealthText2 = diabloOrbHealthText2 or f:CreateFontString("diabloOrbHealthText2", "OVERLAY")
    diabloOrbHealthText2:SetPoint("CENTER",0,-10)
    diabloOrbHealthText2:SetFont(NAMEPLATE_FONT, 12, "THINOUTLINE")
    diabloOrbHealthText2:SetTextColor(1, 1, 1)
    diabloOrbHealthText2:SetText(UnitHealth("player").."/"..UnitHealthMax("player"))
    diabloOrbHealthText2:SetAlpha(1)
    --diabloOrbHealthText2:Hide()
      
      
    diabloOrbManaText = diabloOrbManaText or f2:CreateFontString("diabloOrbManaText", "OVERLAY")
    diabloOrbManaText:SetPoint("CENTER",0,10)
    --diabloOrbManaText:SetFont("Interface\\AddOns\\Diablo3Orbs\\avqest.ttf",16,THICKOUTLINE )
    diabloOrbManaText:SetFont(NAMEPLATE_FONT, 16, "THINOUTLINE")
    diabloOrbManaText:SetTextColor(1, 1, 1)
    diabloOrbManaText:SetText("100%")
    diabloOrbManaText:SetAlpha(1)
    --diabloOrbManaText:Hide()
    diabloOrbManaText2 = diabloOrbManaText2 or f2:CreateFontString("diabloOrbManaText2", "OVERLAY")
    diabloOrbManaText2:SetPoint("CENTER",0,-10)
    diabloOrbManaText2:SetFont(NAMEPLATE_FONT, 12, "THINOUTLINE")
    diabloOrbManaText2:SetTextColor(1, 1, 1)
    diabloOrbManaText2:SetText(UnitMana("player").."/"..UnitManaMax("player"))
    diabloOrbManaText2:SetAlpha(1)
    --diabloOrbManaText2:Hide()
    
    diabloOrbHealthText:Show()
    diabloOrbHealthText2:Show()
    diabloOrbManaText:Show()
    diabloOrbManaText2:Show()


  
    
  end
